using System;
using System.Drawing;
using System.Windows.Forms;

namespace MovingBall
{
    public class SimpleBall : Panel
    {
        private Color _color;
        public int Dx { get; set; }
        public int Dy { get; set; }
        public bool IsEnd { get; set; }


        public SimpleBall(Color color, int size, MouseEventArgs e)
        {
            Location = new Point(e.X, e.Y);
            PresetParameters(color, size);
        }

        public SimpleBall(Color color, int size, int locationX, int locationY)
        {
            Location = new Point(locationX, locationY);
            PresetParameters(color, size);
        }

        private void PresetParameters(Color color, int size)
        {
            IsEnd = false;
            Width = Height = size;
            _color = color;
            Dx = new Random().Next(-10, 10);
            Dy = new Random().Next(-10, 10);
            Paint += BallPaint;
        }

        private void BallPaint(object sender, PaintEventArgs e)
        {
            var graphics = e.Graphics;
            graphics.DrawEllipse(new Pen(_color, Width), 0, 0, Width, Height);
            graphics.Dispose();
        }

        private void CheckBall(Ball ball)
        {
            if (ball == null || Parent == null) return;
            lock (ball)
            {
                if ((ball.Left >= Parent.Width) || (ball.Left <= 0))
                    Dx *= -1;
                if ((ball.Top >= Parent.Height) || (ball.Top <= 0))
                    Dy *= -1;
            }
        }

        private void IncLeft(Ball ball, int value)
        {
            if (ball == null || Parent == null) return;
            lock (ball)
            {
                ball.Left += value;
            }
        }

        private void IncTop(Ball ball, int value)
        {
            if (ball == null || Parent == null) return;
            lock (ball)
            {
                ball.Top += value;
            }
        }
    }
}